package com.cakebackoffice.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakebackoffice.database.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


public class MenuType implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String name;
    private Integer idIllustration;
    private String description;
    private HashMap<Store, LocalPriceHT> localPrice = new HashMap<Store, LocalPriceHT>();
    private HashMap<DeliveryType, RecommendedPrice> recommendedPrice = new HashMap<DeliveryType, RecommendedPrice>();
    private List<ProductType> content = new ArrayList<ProductType>();

    public MenuType(String name) {
        this.name = name;
    }

    private MenuType(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(MenuTypeTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(MenuTypeTable.REMOTE_ID)));
        name = cursor.getString(cursor.getColumnIndexOrThrow(MenuTypeTable.NAME));
        idIllustration = cursor.getInt(cursor.getColumnIndexOrThrow(MenuTypeTable.ILLUSTRATION));
        description = cursor.getString(cursor.getColumnIndexOrThrow(MenuTypeTable.DESCRIPTION));

        //Recuperer les LocalPriceHT
        Cursor cursorLp = dataSource.getMore(this, MenuType_has_LocalPriceHT.getInstance(), MenuType_has_LocalPriceHT.ID_MENUTYPE);
        cursorLp.moveToFirst();
        while (!cursorLp.isAfterLast()) {
            LocalPriceHT l = LocalPriceHT.getOne(dataSource, cursorLp.getLong(cursorLp.getColumnIndexOrThrow(MenuType_has_LocalPriceHT.ID_LOCALPRICEHT)));
            this.localPrice.put(l.getStore(), l);
            cursorLp.moveToNext();
        }

        //Recuperer les RecommendedPrice
        Cursor cursorRp = dataSource.getMore(this, MenuType_has_RecommendedPrice.getInstance(), MenuType_has_RecommendedPrice.ID_MENUTYPE);
        cursorRp.moveToFirst();
        while (!cursorRp.isAfterLast()) {
            RecommendedPrice r = RecommendedPrice.getOne(dataSource, cursorRp.getLong(cursorRp.getColumnIndexOrThrow(MenuType_has_RecommendedPrice.ID_RECOMMENDEDPRICE)));
            this.recommendedPrice.put(r.getDeliveryType(), r);
            cursorRp.moveToNext();

        }

        //Recuperer les ids des Produits
        Cursor cursorp = dataSource.getMore(this, MenuType_has_ProductType.getInstance(), MenuType_has_ProductType.ID_MENUTYPE);
        cursorp.moveToFirst();
        while (!cursorp.isAfterLast()) {
            this.content.add(ProductType.getOne(dataSource, cursorp.getLong(cursorp.getColumnIndexOrThrow(MenuType_has_ProductType.ID_PRODUCTTYPE))));
            cursorp.moveToNext();
        }

    }

    public MenuType(com.cakebackoffice.clientsoap.contract.MenuType menuType) {
        //TODO: MenuTypes
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getIdIllustration() {
        return idIllustration;
    }

    public void setIdIllustration(Integer idIllustration) {
        this.idIllustration = idIllustration;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public HashMap<Store, LocalPriceHT> getLocalPrice() {
        return localPrice;
    }

    public void setLocalPrice(HashMap<Store, LocalPriceHT> localPrice) {
        this.localPrice = localPrice;
    }

    public HashMap<DeliveryType, RecommendedPrice> getRecommendedPrice() {
        return recommendedPrice;
    }

    public void setRecommendedPrice(HashMap<DeliveryType, RecommendedPrice> recommendedPrice) {
        this.recommendedPrice = recommendedPrice;
    }

    public List<ProductType> getContent() {
        return content;
    }

    public void setContent(List<ProductType> content) {
        this.content = content;
    }

    @Override
    public ContentValues getValues() {

        ContentValues values = new ContentValues();
        values.put(MenuTypeTable.REMOTE_ID, this.getRemoteID());
        values.put(MenuTypeTable.NAME, this.name);
        values.put(MenuTypeTable.ILLUSTRATION, this.idIllustration);
        values.put(MenuTypeTable.DESCRIPTION, this.description);
        return values;
    }

    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        MenuType bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = MenuType.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, MenuTypeTable.getInstance());
        } else {
            dataSource.update(this, MenuTypeTable.getInstance());
            dataSource.delete(this, MenuType_has_LocalPriceHT.getInstance(), MenuType_has_LocalPriceHT.ID_MENUTYPE);
            dataSource.delete(this, MenuType_has_ProductType.getInstance(), MenuType_has_ProductType.ID_MENUTYPE);
            dataSource.delete(this, MenuType_has_RecommendedPrice.getInstance(), MenuType_has_RecommendedPrice.ID_MENUTYPE);
        }
        {


            Iterator<LocalPriceHT> iteL = this.getLocalPrice().values().iterator();
            while (iteL.hasNext()) {
                LocalPriceHT l = iteL.next();
                dataSource.insert(MenuType_has_LocalPriceHT.getInstance(), this.getIdDB(), l.getIdDB());
            }

            for (ProductType p : this.getContent()) {
                dataSource.insert(MenuType_has_ProductType.getInstance(), this.getIdDB(), p.getIdDB());
            }

            Iterator<RecommendedPrice> iteR = this.getRecommendedPrice().values().iterator();
            while (iteR.hasNext()) {
                RecommendedPrice r = iteR.next();
                dataSource.insert(MenuType_has_RecommendedPrice.getInstance(), this.getIdDB(), r.getIdDB());
            }

        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, MenuTypeTable.getInstance());
            dataSource.delete(this, MenuType_has_LocalPriceHT.getInstance(), MenuType_has_LocalPriceHT.ID_MENUTYPE);
            dataSource.delete(this, MenuType_has_ProductType.getInstance(), MenuType_has_ProductType.ID_MENUTYPE);
            dataSource.delete(this, MenuType_has_RecommendedPrice.getInstance(), MenuType_has_RecommendedPrice.ID_MENUTYPE);
        }
    }


    public static MenuType getOne(Context context, Long idDB) {

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, MenuTypeTable.getInstance());
        cursor.moveToFirst();
        MenuType item = null;
        while (!cursor.isAfterLast()) {
            item = new MenuType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static MenuType getOneFromRemoteID(Context context, Long idDB) {

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, MenuTypeTable.getInstance());
        cursor.moveToFirst();
        MenuType item = null;
        while (!cursor.isAfterLast()) {
            item = new MenuType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<MenuType> getAll(Context context) {
        List<MenuType> result = new ArrayList<MenuType>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(MenuTypeTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new MenuType(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static MenuType getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, MenuTypeTable.getInstance());
        cursor.moveToFirst();
        MenuType item = null;
        while (!cursor.isAfterLast()) {
            item = new MenuType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
